<template>
  <div class="sms-login">
    <div class="fy-content">
      <div class="fy_content">
        <!-- 注册/验证码登录 logo -->
        <div class="fy_top">
          <img
            src="https://msmk2019.oss-cn-shanghai.aliyuncs.com/uploads/image/2019pILfAg7Avr1567732916.png"
            alt
          />
        </div>
        <!-- 注册/验证码登录 主体 -->
        <div class="fy_num">
          <input type="text" maxlength="11" placeholder="请输入手机号" v-model="fy_zc_sjh" />
        </div>
        <div class="fy_num">
          <input type="password" placeholder="请输入短信验证码" v-model="zjb_yan" />
          <span class="fy_sms" @click="zjb_Code">获取验证码</span>
        </div>
        <div class="fy_others">
          <span>*未注册的手机号将自动注册</span>
          <!-- 点击跳转到登录页面 -->
          <span @click="fy_toLogin()">使用密码登录</span>
        </div>
      </div>
      <!-- 登录按钮 -->
      <div class="fy_login">
        <!-- 点击跳转到设置密码路由 -->
        <van-button
          round
          type="warning"
          eb6100
          size="large"
          class="fy_button"
          @click="fy_toSetpass()"
        >登录</van-button>
      </div>
    </div>
    {{zjb_list}}
  </div>
</template>

<script>
export default {
  name: "sms-login",
  data() {
    return {
      fy_zc_sjh: "",
      zjb_list: "",
      zjb_yan: ""
    };
  },
  methods: {
    //--->点击获取验证码
    zjb_Code() {
      //1.点击获取验证码的时候，先判断手机号的格式对不对；
      var ipone = /^[1]([3-9])[0-9]{9}$/;
      var test = ipone.test(this.fy_zc_sjh);
      if (test) {
        this.axios
          .post("https://test.365msmk.com/api/app/smsCode", {
            mobile: this.fy_zc_sjh,
            sms_type: "login"
          })
          .then(res => {
            console.log(res);
            this.zjb_list = res.data.code; //--->状态为200；验证码后台已验证
          });
      } else {
        this.$toast("手机号码格式不对");
        return false;
      }
    },
    // 跳转到登录页面
    fy_toLogin() {
      this.$router.push("/login");
    },
    // 跳转到设置密码页面
    fy_toSetpass() {
      //-->点击注册
      //1.判断验证码是否是纯数字加长度2.判断验证码是否一致，3.点击跳转
      var yan = /^\d{6}$/;
      var test = yan.test(this.zjb_yan);
      if (test) {
        this.axios
          .post("https://test.365msmk.com/api/app/login", {
            mobile: this.fy_zc_sjh,
            sms_code: this.zjb_yan,
            type: 2
          })
          .then(ret => {
            if (ret.data.code == 200) {
              if (ret.data.data.is_new == 1) {//--->判断是否设置过密码，没有字段为1
                this.$router.push("/set-pass");
                //--->用浏览器储存用户输入的手机号
                sessionStorage.setItem("ipone", this.fy_zc_sjh);
                //--->用浏览器储存用户输入的验证码
                sessionStorage.setItem("yan", this.zjb_yan);
                //--->用浏览器储存用户登录后获取的令牌，token
                sessionStorage.setItem("token", ret.data.data.remember_token);
              } else if(ret.data.data.is_new==2){//--->要是设置过密码，直接跳转到首页，字段为2
                this.$router.push("/");//--->跳转到首页
                //--->用浏览器储存用户输入的手机号
                sessionStorage.setItem("ipone", this.fy_zc_sjh);
                //--->用浏览器储存用户输入的验证码
                sessionStorage.setItem("yan", this.zjb_yan);
                //--->用浏览器储存用户登录后获取的令牌，token
                sessionStorage.setItem("token", ret.data.data.remember_token);
              }
            } else {
              this.$toast("验证码格式不对");
              return false;
            }
            //  console.log('====================================');
            //  console.log(ret);
            //  console.log('====================================');
          });
      } else {
        this.$toast("验证码格式不对");
        return false;
      }
    }
  }
};
</script>

<style lang='scss' scoped>
html,
body,
.sms-login {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #fff;
  .fy-content {
    width: 100%;
    overflow: auto;
    flex: 1;
    .fy_content {
      width: 100%;
      .fy_top {
        width: 100%;
        img {
          width: 60%;
          margin: 1rem auto;
          display: block;
        }
      }
      .fy_num {
        font-size: 0.3rem;
        width: 5rem;
        margin: 0 auto;
        position: relative;
        input {
          border: 0;
          display: block;
          margin: 0 auto;
          box-sizing: border-box;
          padding: 0.3rem 0;
          height: 100%;
          font-size: 0.25rem;
          font-family: PingFangSC-Regular;
          font-weight: 400;
          line-height: 0.3rem;
          float: left;
          outline: none;
        }
        .fy_sms {
          position: absolute;
          right: 0;
          top: 0.3rem;
          color: orange;
          font-size: 0.2rem;
        }
      }
      .fy_others {
        width: 5rem;
        display: flex;
        justify-content: space-between;
        margin: 0 auto;

        span {
          font-size: 0.2rem;
          font-family: PingFangSC-Regular;
          font-weight: 400;
          color: #999;
        }
      }
    }
    .fy_login {
      width: 6.4rem;
      height: 1.2rem;
      text-align: center;
      .fy_button {
        width: 80%;
        font-size: 0.25rem;
        margin-left: 0;
      }
    }
  }
}
</style>
